package com.prtek.reminder.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author Jarly
 * @date 2018年7月26日
 */
public abstract class BaseListAdapter<T> extends BaseAdapter {
	protected List<T> mDataList = new ArrayList<>();


	protected Context mContext;

	public BaseListAdapter(Context context) {
		mContext = context;
	}

	public void setDataList(List<T> dataList) {
		mDataList.clear();
		mDataList.addAll(dataList);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mDataList.size();
	}

	@Override
	public Object getItem(int position) {
		return mDataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder ;
		if (convertView == null) {
			viewHolder = onCreateViewHolder(position);
			convertView = viewHolder.mView;
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		onBindViewHolder(viewHolder, position);

		return convertView;
	}

	

	public class ViewHolder {
		public View mView;
		public ViewHolder(View view) {
			mView = view;
		}

	}

	protected abstract ViewHolder onCreateViewHolder(int position);
	
	protected abstract void onBindViewHolder(ViewHolder viewHolder, int position);
	
	public List<T> getDataList() {
		return mDataList;
	}
}
